package flinkSql.window.group;

import bean.SensorReading;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.Slide;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
import org.apache.flink.types.Row;
import org.junit.Test;

import static org.apache.flink.table.api.Expressions.$;
import static org.apache.flink.table.api.Expressions.lit;

//TODO 处理时间的滑动窗口
public class Flink_ProcessTime_GroupWindow_Slide {
    public static void main(String[] args) throws Exception {
        //获取执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment().setParallelism(1);

        //获取table执行环境
        StreamTableEnvironment tableEnv = StreamTableEnvironment.create(env);

        //从端口获取数据流并转换
        DataStreamSource<String> input = env.socketTextStream("hadoop102", 9999);
        SingleOutputStreamOperator<SensorReading> mapDS = input.map(new MapFunction<String, SensorReading>() {
            @Override
            public SensorReading map(String value) throws Exception {
                String[] split = value.split(",");
                return new SensorReading(split[0], Long.parseLong(split[1]), Double.parseDouble(split[2]));
            }
        });

        //基于时间的滚动窗口
        Table table = tableEnv.fromDataStream(mapDS, "id,ts,temp,pt.proctime");
        //table API
        //滑动窗口
        Table tableResult = table.window(Slide.over("10.seconds").every("5.seconds").on("pt").as("sw"))
                .groupBy("sw,id")
                .select("id,id.count");

        //SQL 窗口
        tableEnv.createTemporaryView("sensorTable",table);
        Table sqlResult = tableEnv.sqlQuery("select id,count(id) from sensorTable group by id,hop(pt,interval '5' second,interval '10' second)");

        //表转流打印结果
        tableEnv.toAppendStream(tableResult, Row.class).print("table");
        tableEnv.toAppendStream(sqlResult,Row.class).print("sql");

        //执行
        env.execute();

    }

    @Test
    public void test() throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment().setParallelism(1);
        StreamTableEnvironment tableEnvironment = StreamTableEnvironment.create(env);

        SingleOutputStreamOperator<SensorReading> input = env.socketTextStream("node193", 9999)
                .map(new MapFunction<String, SensorReading>() {
                    @Override
                    public SensorReading map(String value) throws Exception {
                        String[] split = value.split(",");
                        return new SensorReading(split[0], Long.parseLong(split[1]), Double.parseDouble(split[2]));
                    }
                });

        Table table = tableEnvironment.fromDataStream(input, $("id"), $("ts"), $("temp"), $("pt").proctime());

        Table select1 = table.window(Slide.over(lit(10).seconds()).every(lit(5).seconds()).on($("pt")).as("pt"))
                .groupBy($("id"), $("pt"))
                .select($("id"), $("temp").sum());

        tableEnvironment.createTemporaryView("sqlTable",table);
        Table select2 = tableEnvironment.sqlQuery("select id,count(id) from sqlTable group by id,hop(pt,interval '5' second,interval '10' second)");

        tableEnvironment.toAppendStream(select1,Row.class).print("table");
        tableEnvironment.toAppendStream(select2,Row.class).print("sql");

        env.execute();

    }
}
